package test_autoencode;

import java.io.IOException;
import java.util.Scanner;

/**
 * A class that handles which emulator to run and with which settings.
 * @author Raiscan
 */
public class Emulator {

    private String movieFile, romName, fceuxPath;
    private int endGame;

    /**
     * Constructer class.
     * @param movieFile
     * @param romName
     * @param endGame
     */
    public Emulator(String fceuxPath, String movieFile, String romName, int endGame) {
        this.fceuxPath = fceuxPath;
        this.endGame = endGame;
        this.movieFile = movieFile;
        this.romName = romName;
    }

    public boolean runEmulator() {
        try {
            //TODO: Complete this execution line!
            Process p = Runtime.getRuntime().exec(fceuxPath+" -playmovie "
                    + movieFile + " -nothrottle -avi capture.avi \"" + romName + "\"");
            OutputSoaker inStream = new OutputSoaker ("stin",p.getInputStream());
            OutputSoaker errStream = new OutputSoaker ("sterr",p.getErrorStream());
            inStream.start();
            errStream.start();
            int exitcode = p.waitFor();
            return exitcode == 0;
        } catch (IOException e)
        {
            System.err.println("Error Running FCEUX. " +
                    "Please check config.properties contains the correct path " +
                    "to FCEUX.");
        }

        catch (Exception e) {
            System.err.println("error during capture");
            e.printStackTrace();
        }
        return false;
    }
}
